package com.iwhalecloud.bss.kite.cucc.service.order;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneProdInstService;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.ProdInstDTO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.order.ITransferPlanService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.TransferPlanVO;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.OfferDetailMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.ProductMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.scene.bo.OperateSceneProdInstBO;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TransferPlanService implements ITransferPlanService {

    @Autowired
    private IOperateSceneProdInstService operateSceneProdInstService;

    @Autowired
    private OperateSceneProdInstBO operateSceneProdInstBO;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Resource
    private OfferDetailMapper offerDetailMapper;

    @Resource
    private ProductMapper productMapper;

    /**
     * 查询传输计划
     *
     * @param sceneInstId
     * @return
     */
    @Override
    public List<TransferPlanVO> getTransferPlanInfo(String sceneInstId) {
        List<TransferPlanVO> transferPlanVOs = new ArrayList<TransferPlanVO>();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);

        kiteProdInstList.forEach(prodInst -> {
            Product product = ProductCache.get(prodInst.getProdId());
            if (KiteStringUtils.isEqual(KeyConsts.PROD_FUNC_TYPE_105, product.getProdFuncType())) {
                TransferPlanVO transferPlanVO = new TransferPlanVO();
                transferPlanVO.setTransferTaskInstId(prodInst.getProdInstId());
                List<InstAttrDTO> accessProdInstAttr = CopyToDtoUtil.transInstAttrDtoList(prodInst.listAttrs());
                transferPlanVO.setPlanAttrs(accessProdInstAttr);
                transferPlanVOs.add(transferPlanVO);
            }
        });
        return transferPlanVOs;
    }

    /**
     * 查询传输计划
     *
     * @param sceneInstId
     * @return
     */
    public TransferPlanVO getTransferPlanInfoByProdInstId(String sceneInstId, String prodInstId) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        TransferPlanVO transferPlanVO = new TransferPlanVO();
        kiteProdInstList.forEach(prodInst -> {
            Product product = ProductCache.get(prodInst.getProdId());
            if (KiteStringUtils.isEqual(KeyConsts.PROD_FUNC_TYPE_105, product.getProdFuncType())
                && KiteStringUtils.isEqual(prodInst.getProdInstId(), prodInstId)) {
                transferPlanVO.setTransferTaskInstId(prodInst.getProdInstId());
                List<InstAttrDTO> accessProdInstAttr = CopyToDtoUtil.transInstAttrDtoList(prodInst.listAttrs());
                transferPlanVO.setPlanAttrs(accessProdInstAttr);
            }
        });
        return transferPlanVO;
    }

    /**
     * 新增传输计划
     *
     * @param sceneInstId
     * @return
     */
    @Override
    public TransferPlanVO addTransferPlanInfo(String sceneInstId, String custId, String offerInstId, String roleId,
        String additionalOfferId) {

        KiteOfferInst kiteOfferInst = querySceneInstService.getOfferInst(custId, offerInstId);

        List<OfferDetail> offerDetails = offerDetailMapper.listOfferDetails(kiteOfferInst.getOfferId());
        ProdInstDTO prodInstDTO = new ProdInstDTO();
        for (OfferDetail offerDetail : offerDetails) {
            Product product = productMapper.selectinfoByProdId(offerDetail.getObjId());
            if (KiteStringUtils.isEqual(KeyConsts.PROD_FUNC_TYPE_105, product.getProdFuncType())) {
                prodInstDTO = operateSceneProdInstService.addProdRole(sceneInstId, ContextUtil.getUserId().toString(),
                    custId, offerInstId, product.getProdId(), roleId, "");
            }
        }

        return this.getTransferPlanInfoByProdInstId(sceneInstId, prodInstDTO.getProdInstId());
    }

    /**
     * 修改传输计划
     *
     * @param sceneInstId
     * @return
     */
    @Override
    public TransferPlanVO updateTransferPlanInfo(String sceneInstId, String transferPlanInstId, String fieldName,
        String newValue, String valueDesc, String custId, String staffId, String extJson) {

        operateSceneProdInstBO.updateProdInstAttr(sceneInstId, transferPlanInstId, fieldName, newValue, valueDesc,
            custId, staffId, extJson);

        return this.getTransferPlanInfoByProdInstId(sceneInstId, transferPlanInstId);
    }
}
